package com.javaweb.controller;

import com.javaweb.controller.common.ArticleVo;
import com.javaweb.controller.common.ResponseResult;
import com.javaweb.service.ArticleService;
import com.javaweb.service.dto.ArticleConditionDto;
import com.javaweb.service.dto.ArticleDto;
import com.javaweb.service.dto.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/10/21 22:15
 */

@RestController
@RequestMapping("/articles")
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/test")
    public ResponseEntity<String> test() {
        return ResponseEntity.ok("add article success") ;
    }


    public ResponseEntity<String> addArticle(@RequestBody ArticleVo articleVo) {
        log.debug("recieve articleVo:{}", articleVo);
        return ResponseEntity.ok("add article success") ;

    }

    @PostMapping("/page")
    public ResponseResult<PageResult<ArticleVo>> queryArticles(@ModelAttribute ArticleConditionDto articleCond,
                                                    @RequestParam(defaultValue = "0") Integer pageNum,
                                                               @RequestParam(defaultValue = "10")Integer pageSize) {
        log.info("recieve articleCond:{},pageNum:{},pageSize:{}", articleCond, pageNum, pageSize) ;
        PageResult<ArticleDto> articleDtoPageResult = articleService.queryByArticleCondition(articleCond, pageNum, pageSize);
        return ResponseResult.ok(ArticleVo.toArticleVo(articleDtoPageResult));
    }

    /**:
     * TASK: todo
     * 1. 参考 chpt_04_springmvc的 ValidateController
     * 2. 实现ArticleController的newArticle(ArticleDto articleDto) 并验证 articleDto中的tile 非空且长度《=200
     * 3. 使用APIFOX进行接口测试
     */




}
